/*
 * AS3 MultiTouch Client
 *
 * DragBehaviour - Moves the object to the cursor position  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * DragBehaviour - Moves the object to the cursor position 
 * Uses caurina tweener for moving.  
 */
package de.artifacts.multitouch.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	
	public class ScaleBehaviour extends TwoFingerBehaviour
	{		
		public static const DISTANCE_SCALE_FACTOR : Number = 100;
		public var minScale : Number = 1;
		public var maxScale : Number = 2;
		public var initialScale : Number;
		
		override public function touchedHandler(event : TouchEvent) : void
		{
			super.touchedHandler(event);			
			if (this.touchingCursors.length < 2) return;
			var cursor1 : Cursor = this.touchingCursors[0];
			var cursor2 : Cursor = this.touchingCursors[1];
			var distance : Number = manhattanDistance(cursor1.position, cursor2.position);
			initialScale = distance / DISTANCE_SCALE_FACTOR - touchable.wrappee.scaleX;
		}
		
		override public function repositionedHandler(event : TouchEvent) : void
		{
			if (this.touchingCursors==null || this.touchingCursors.length < 2) return;
			var cursor1 : Cursor = this.touchingCursors[0];
			var cursor2 : Cursor = this.touchingCursors[1];
			
			var distance : Number =  manhattanDistance(cursor1.position, cursor2.position);
			var scale : Number = distance / DISTANCE_SCALE_FACTOR - initialScale;
			
			touchable.wrappee.scaleX = Math.min(Math.max(minScale, scale), maxScale);
			touchable.wrappee.scaleY = Math.min(Math.max(minScale, scale), maxScale);
			
		}				
		
	}
}